<template>
	<view class="animal_collect">
		<view class="animal_header">
			<view class="search_header_back">
				<image @click="backGo" src="/static/back.png" mode=""></image>
			</view>
			<view class="animal_header_title">
				<view class="th2">Web for
				</view>
				<view class="th1">You</view>
			</view>
			<image aspectFit class="index_header_img" src="/static/col-bg.png" mode=""></image>
		</view>
		<view class="animal_main">
			<view class="collect_item" v-for="(item,index) in webList">
				<view class="item_left">
					<image @click="jumpGo(item.did)" :src="item.pet_coverURL" mode="aspectFill"></image>
				</view>
				<view class="item-center">
					<view class="tdesc_tit">
						{{item.pet_name}}
					</view>
					<view class="tdesc_bot">
						{{item.pet_engName}}
					</view>
					<view class="tdesc_bot">
						{{item.pet_life}}
					</view>

				</view>
				<view class="item-right">
					<view class="changeCol" @click="changeCol(index,item['flat'])" :class="item.flat?'active':''">
						<image src="/static/collect.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Tabbar from '@/components/tabbar.vue'
	import {
		userWebList,
		judgmentUserCol,
		addCol
	} from "../../api/index.js"
	export default {
		data() {
			return {
				webList: []
			};
		},
		onShow() {
			let uid = uni.getStorageSync("uid")
			userWebList(uid).then((content) => {
				let {
					data: {
						code,
						data
					}
				} = content
				let uid = uni.getStorageSync("uid")
				this.selCol(uid, data)
				// this.webList = data
				// this.
				console.log(code, data)
			})
		},
		methods: {
			changeCol(idx, flat) {
				this.webList[idx].flat = !flat

				let uid = uni.getStorageSync("uid");
				let did = this.webList[idx].did
				addCol(uid, did).then((content) => {
					console.log(content.data.msg)
				})

			},
			jumpGo(id) {
				uni.setStorageSync("id", id)
				uni.navigateTo({
					url: `/pages/detail/detail`
				})

				console.log(id)
			},
			async selCol(uid, arr) {
				let newArr = Promise.all(
					arr.map((item, index) => {

						return (async () => {
							await judgmentUserCol(uid, item.did).then((content) => {
								arr[index].flat = content.data.flat
							})
							return item
						})()
					})
				).then((res) => {
					console.log(res)
					this.webList = res
					uni.hideLoading()
				})
			},

			backGo() {
				uni.navigateBack({
					delta: 1
				})
			},
		},

	}
</script>

<style lang="scss">
	.animal_collect {
		height: 100vh;
		background-color: #a1b275;
	}

	.search_header_back {
		position: absolute;
		top: 10px;
		left: 10px;
		z-index: 1;

		image {

			width: 30px;
			height: 30px;
		}
	}

	.animal_search {
		margin: 0 15px;
		height: 50px;
		background-color: white;
		border-radius: 16px;
		display: flex;
		align-items: center;
		background-color: #ffffff;

		.search_icon {
			width: 20px;
			height: 20px;
			margin: 10px;
		}

		.sPlo {
			font-size: 14px;
			color: #BBC3CE;
			font-weight: 500;
		}

	}

	.animal_header {
		width: 100%;
		height: 150px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.index_header_img {
		display: block;
		width: 172px;
		height: 100px;
		margin-right: 20px;
		margin-top: 10px;
	}

	.animal_header_title {
		margin-top: 20px;
		margin-left: 40px;
		color: white;
		line-height: 36px;

		.th2 {
			font-size: 28px;
			font-weight: 100;
		}

		.th1 {
			font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
			font-size: 28px
		}
	}

	.animal_main {
		border-radius: 43px 43px 0 0;
		background-color: #f3f2f2;
		min-height: 517px;
		display: flex;
		flex-direction: column;
		align-items: center;

		.collect_item {
			width: 333px;
			height: 96px;
			border-radius: 17px;
			background-color: white;
			margin-bottom: 20px;
			display: flex;
			align-items: center;


			&:first-child {
				margin-top: 30px;
			}

			.item_left {
				margin-left: 4px;
				width: 88px;
				height: 88px;
				background-color: lightblue;
				border-radius: 17px;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.item-center {
				height: 70px;
				width: 150px;
				margin-left: 20px;

				.tdesc_tit {
					font-size: 18px;
					color: #333;
				}

				.tdesc_bot {
					margin-top: 6px;
					font-size: 14px;
					color: gray;
					line-height: 14px;

					&:last-child {
						margin-top: 2px;
					}
				}
			}

			.item-right {
				height: 88px;
				width: 30px;
				margin-left: 20px;
				display: flex;
				align-items: flex-end;
				justify-content: flex-end;
				margin-bottom: 20px;

				.changeCol {
					width: 22px;
					height: 22px;
					background-color: #ECEBEB;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
					transition: .3s;

					&.active {
						background-color: #ffd967;
						box-shadow: 0 2px 6px #ffd967;
					}

					image {
						width: 11px;
						height: 11px;
					}
				}
			}
		}
	}
</style>